<?php
namespace backend\controllers;

use Yii;
use yii\db\Exception;
use yii\web\Controller;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use backend\models\LoginForm;

/**
 * Site controller
 */
class SiteController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::class,
                'rules' => [
                    [
                        'actions' => ['login', 'error'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['logout', 'index'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::class,
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return string
     * @throws Exception
     */
    public function actionIndex()
    {
        $mysqlVersion = Yii::$app->db->createCommand('select version()')->queryScalar();
        //$sevenDayUserData = $userModel->getSevenDayUserShow();
        $sysInfo = [
            '操作系统'     => PHP_OS,
            '运行环境'     => $_SERVER["SERVER_SOFTWARE"],
            'PHP 版本'     => strpos(PHP_VERSION, '-') ? substr(PHP_VERSION, 0, strpos(PHP_VERSION, '-')) : PHP_VERSION,
            //'SWOOLE 版本'  => swoole_version(),
            'MYSQL 版本'   => strpos($mysqlVersion, '-') ? substr($mysqlVersion, 0, strpos($mysqlVersion, '-')) : $mysqlVersion,
            //'PHP运行方式'  => php_sapi_name(),
            '上传附件限制' => ini_get('upload_max_filesize'),
            '执行时间限制' => ini_get('max_execution_time') . "秒",
            'CURL 支持'    => function_exists("curl_getinfo") ? '是' : '否',
            '硬盘剩余空间' => round((@disk_free_space(".") / (1024 * 1024)), 2) . 'M',
            '时区'        => Yii::$app->getTimeZone(),
            //'开发人员'    => 'Go',
            //'访问者IP'    => Yii::$app->request->getUserIP(),
            //'访问者地址'  => $locationInfo['country'] . $locationInfo['area']
        ];
        return $this->render('index', [
            'sysInfo' => $sysInfo,
            'userShow' => [
                'totalUser' => 0,
                'yesterdayRegisterNum' => 0,
                'todayRegisterNum' => 0,
            ],
            'orderMoneyShow' => [
                'totalOrder' => 0,
                'todayOrderNum' => 0,
                'monthOrderNum' => 0,
            ],
            'orderProductShow' => [
                'totalOrder' => 0,
                'todayOrderNum' => 0,
                'monthOrderNum' => 0,
            ],
        ]);
    }

    /**
     * Login action.
     *
     * @return string
     */
    public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }
        $this->layout = 'main-login';
        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        } else {
            $model->password = '';

            return $this->render('login', [
                'model' => $model,
            ]);
        }
    }

    /**
     * Logout action.
     *
     * @return string
     */
    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }
}
